﻿using System;
using AgentListener.Controllers.Strategies;
using AgentListener.Mongo;
using Common.Annotations;

namespace AgentListener.Controllers
{
    internal class ControllerFactory : IControllerFactory
    {
        private readonly IMongoProvider _mongoProvider;

        public ControllerFactory([NotNull] IMongoProvider mongoProvider)
        {
            if (mongoProvider == null) throw new ArgumentNullException("mongoProvider");
            _mongoProvider = mongoProvider;
        }

        /// <summary>
        /// Создает контроллер исходя из запроса
        /// </summary>
        public IRequestController GetController(HttpDataHolder data)
        {
            if (data == null) throw new ArgumentNullException("data");

            IRequestStrategy strategy;

            switch (data.Context.Request.Url.LocalPath.ToLower())
            {
                case "/sync":
                    strategy = new SyncStrategy(_mongoProvider);
                    break;
                case "/data":
                    strategy = new DataStrategy(_mongoProvider);
                    break;
                case "/reg":
                    strategy = new RegisterStrategy(_mongoProvider);
                    break;
                case "/getmetrics":
                    strategy = new GetMetricsStrategy(_mongoProvider);
                    break;
                default:
                    strategy = new WrongRequestStrategy();
                    break;
            }

            return new RequestController(data, strategy);
        }
    }
}